package com.houtai.controller.course;

import com.houtai.authority.Authority;
import com.houtai.authority.AuthorityType;
import com.houtai.base.ResultVo;
import com.houtai.bean.dto.course.TdCourseDetailDto;
import com.houtai.bean.dto.course.TdCourseListDto;
import com.houtai.bean.dto.course.TdCourseSignUpDto;
import com.houtai.bean.dto.course.collection.TdCourseCollectionInsertDto;
import com.houtai.bean.dto.course.comments.TdCourseCommentsInsertDto;
import com.houtai.bean.dto.course.comments.TdCourseCommentsListDto;
import com.houtai.bean.vo.course.TdCourseDetailVo;
import com.houtai.bean.vo.course.TdCourseListVo;
import com.houtai.bean.vo.course.comments.TdCourseCommentsListVo;
import com.houtai.bean.vo.dict.DictListResult;
import com.houtai.service.activity.works.ActivityWorksService;
import com.houtai.service.course.CourseService;
import com.houtai.service.dict.DictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@Api(value="课程接口", tags={"课程接口"})
@RequestMapping(value = {"api/v1"}, method = RequestMethod.POST)
public class CourseContorller {
    @Autowired
    private DictService dictService;
    @Autowired
    private ActivityWorksService activityWorksService;

    @Autowired
    private CourseService courseService;

    @Authority(value = AuthorityType.NoValidate)
    @ApiOperation(value="课程类别列表", notes="课程类别列表")
    @RequestMapping(value = "/course/type/list", method = RequestMethod.POST)
    public DictListResult type() {
        return dictService.findDictByType("course_type");
    }

    @ApiOperation(value="课程列表", notes="课程列表")
    @Authority(AuthorityType.NoValidate)
    @RequestMapping(value = "/course/list")
    public TdCourseListVo list(@ApiParam(value = "课程列表所需参数") @Valid @RequestBody TdCourseListDto dto, BindingResult result) {
        TdCourseListVo retInfo = new TdCourseListVo();
        if (result.hasErrors()) {
            List<FieldError> err = result.getFieldErrors();
            FieldError fe = err.get(0);
            retInfo.setMark("1");
            retInfo.setTip(fe.getDefaultMessage());
        } else {
            retInfo = courseService.activityList(dto);
        }
        return retInfo;
    }
    @ApiOperation(value="课程详情", notes="课程详情")
    @Authority(AuthorityType.NoValidate)
    @RequestMapping(value = "/course/detail")
    public TdCourseDetailVo detail(@ApiParam(value = "课程详情所需参数") @Valid @RequestBody TdCourseDetailDto dto, BindingResult result,
                                   @RequestHeader String token) {
        TdCourseDetailVo retInfo = new TdCourseDetailVo();
        if (result.hasErrors()) {
            List<FieldError> err = result.getFieldErrors();
            FieldError fe = err.get(0);
            retInfo.setMark("1");
            retInfo.setTip(fe.getDefaultMessage());
        } else {
            retInfo = courseService.activityDetail(dto, token);
        }
        return retInfo;
    }
    @ApiOperation(value="课程报名", notes="课程报名")
    @Authority(AuthorityType.Login)
    @RequestMapping(value = "/course/sign/up")
    public ResultVo sign(@ApiParam(value = "课程详情所需参数") @Valid @RequestBody TdCourseSignUpDto dto, BindingResult result,
                         @RequestHeader String token) {
        ResultVo retInfo = new ResultVo();
        if (result.hasErrors()) {
            List<FieldError> err = result.getFieldErrors();
            FieldError fe = err.get(0);
            retInfo.setMark("1");
            retInfo.setTip(fe.getDefaultMessage());
        } else {
            retInfo = courseService.signUp(dto, token);
        }
        return retInfo;
    }
    @ApiOperation(value="课程评价", notes="课程评价")
    @Authority(AuthorityType.Login)
    @RequestMapping(value = "/course/comments")
    public ResultVo comments(@ApiParam(value = "课程评价所需参数") @Valid @RequestBody TdCourseCommentsInsertDto dto, BindingResult result,
                             @RequestHeader String token) {
        ResultVo retInfo = new ResultVo();
        if (result.hasErrors()) {
            List<FieldError> err = result.getFieldErrors();
            FieldError fe = err.get(0);
            retInfo.setMark("1");
            retInfo.setTip(fe.getDefaultMessage());
        } else {
            retInfo = courseService.comments(dto, token);
        }
        return retInfo;
    }
    @ApiOperation(value="课程评价列表", notes="课程评价列表")
    @Authority(AuthorityType.NoValidate)
    @RequestMapping(value = "/course/comments/list")
    public TdCourseCommentsListVo comments(@ApiParam(value = "课程评价列表所需参数") @Valid @RequestBody TdCourseCommentsListDto dto, BindingResult result,
                                           @RequestHeader String token) {
        TdCourseCommentsListVo retInfo = new TdCourseCommentsListVo();
        if (result.hasErrors()) {
            List<FieldError> err = result.getFieldErrors();
            FieldError fe = err.get(0);
            retInfo.setMark("1");
            retInfo.setTip(fe.getDefaultMessage());
        } else {
            retInfo = courseService.courseCommentsList(dto);
        }
        return retInfo;
    }
    @ApiOperation(value="课程收藏", notes="课程收藏")
    @Authority(AuthorityType.Login)
    @RequestMapping(value = "/course/collection")
    public ResultVo collection(@ApiParam(value = "课程收藏所需参数") @Valid @RequestBody TdCourseCollectionInsertDto dto, BindingResult result,
                               @RequestHeader String token) {
        ResultVo retInfo = new ResultVo();
        if (result.hasErrors()) {
            List<FieldError> err = result.getFieldErrors();
            FieldError fe = err.get(0);
            retInfo.setMark("1");
            retInfo.setTip(fe.getDefaultMessage());
        } else {
            retInfo = courseService.collection(dto, token);
        }
        return retInfo;
    }
    @ApiOperation(value="取消课程收藏", notes="取消课程收藏")
    @Authority(AuthorityType.Login)
    @RequestMapping(value = "/course/comments/cancel")
    public ResultVo cancel(@ApiParam(value = "取消课程收藏所需参数") @Valid @RequestBody TdCourseCollectionInsertDto dto, BindingResult result,
                             @RequestHeader String token) {
        ResultVo retInfo = new ResultVo();
        if (result.hasErrors()) {
            List<FieldError> err = result.getFieldErrors();
            FieldError fe = err.get(0);
            retInfo.setMark("1");
            retInfo.setTip(fe.getDefaultMessage());
        } else {
            retInfo = courseService.collectionCancel(dto, token);
        }
        return retInfo;
    }
}
